<?php
namespace app\index\controller;
use think\Controller;
//use think\Request;
use think\Db;
use think\Session;

class Login extends Controller{
	
   //登录页面展示页
    public function login(){
     return $this->fetch();
    }
	
	//用户登录
	public function login_add(){
     $arr = input();
		 $num = $arr['num'];
		 $pwd = $arr['pwd'];
		 $num_arr = Db::table('think_user')->where('mobile','EQ',$num)->field('mobile')->find();
		 if($num_arr){
	     $pwd_new = Db::table('think_user')->where('mobile','EQ',$num)->field('pasword')->find();
		 if(password_verify(md5($pwd),$pwd_new['pasword'])){
		  	$data['code'] = 2;
			$data['msg']='登录成功';
		    $date = Db::table('think_user')->where('mobile','EQ',$num)->field('id,mobile,username,user_type_id')->find();
			Session::set('date',$date);
			return $data;
		  }else{
		  	$data['code'] = 1;
		    $data['msg']='密码错误';
			return $data;
		  }
		 }else{
		 	$data['code'] = 1;
			$data['msg']='没有此账号，请注册';
			return $data;
		 }
				
    }
	
	
   public function forget()
    {
      return $this->fetch();
    }
	
	public function register()
    {
     return $this->fetch();
    }
	
	//用户注册
	public function addUser(){
			$arr = input();
			$num = $arr['num'];
			$paw = $arr['pwd'];
			$surepwd = $arr['surepwd'];
			
			if($paw!=$surepwd){
				$data['code'] = 1;
				$data['msg']='两次密码不一样';
				return $data;
			}
			$num_arr = Db::table('think_user')->where('mobile','EQ',$num)->field('mobile')->find();
			//验证手机号
			$isMatched = preg_match('/^0?(13|14|15|17|18)[0-9]{9}$/', $num, $matches);
			if(!$isMatched){
				$data['code'] = 1;
				$data['msg']='请输入正确的手机号';
				return $data;
			}
			if($num_arr){
				$data['code'] = 1;
				$data['msg']='此手机号以注册';
				return $data;
			}else{
				$new_arr['mobile'] = $num;
				$new_arr['pasword'] =  password_hash(md5($paw), PASSWORD_DEFAULT);
				$new_arr['add_time']=time();
				$res =Db::table('think_user')->insert($new_arr);
					 if($res){
					 	$data['code'] = 2;
						$data['msg']='注册成功';
						return $data;
					  }else{
						 	$data['code'] = 1;
							$data['msg']='注册失败';
						    return $data;
					  }
			}
    }

  
    //忘记密码
    public function updatepass(){
			$arr = input();
			$num = $arr['num'];
			$paw = $arr['pwd'];
			$surepwd = $arr['surepwd'];
			if($paw!=$surepwd){
				$data['code'] = 1;
				$data['msg']='两次密码不一样';
				return $data;
			}
			$num_arr = Db::table('think_user')->where('mobile','EQ',$num)->field('mobile')->find();
			//验证手机号
			$isMatched = preg_match('/^0?(13|14|15|17|18)[0-9]{9}$/', $num, $matches);
			if(!$isMatched){
				$data['code'] = 1;
				$data['msg']='请输入正确的手机号';
				return $data;
			}
			if($num_arr){
				if($paw!=$surepwd){
				$data['code'] = 1;
				$data['msg']='两次密码不一样';
				return $data;
			    }else{
				$new_arr['pasword'] =  password_hash(md5($paw), PASSWORD_DEFAULT);
				$res =Db::table('think_user')->where('mobile','EQ',$num)->setField($new_arr);
				   if($res){
					 	$data['code'] = 2;
						$data['msg']='修改成功';
						return $data;
					}else{
						 	$data['code'] = 1;
							$data['msg']='修改失败';
						    return $data;
					}
				
			   }
			}else{
				$data['code'] = 1;
				$data['msg']='此账号没有注册';
				return $data;
			}
    }

    
}
